package com.sqx.modules.timedtask;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jfinal.weixin.sdk.api.TemplateData;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.postPush.entity.PostPush;
import com.sqx.modules.postPush.service.PostPushService;
import com.sqx.modules.pushMsgType.entity.PushMsgType;
import com.sqx.modules.pushMsgType.service.PushMsgTypeService;
import com.sqx.modules.utils.SenInfoCheckUtil;
import com.sqx.modules.wxMsg.controller.WeiXinMsgController;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import weixin.popular.api.MessageAPI;
import weixin.popular.bean.message.templatemessage.TemplateMessage;
import weixin.popular.bean.message.templatemessage.TemplateMessageItem;
import weixin.popular.bean.message.templatemessage.TemplateMessageResult;
import weixin.popular.support.TokenManager;

import javax.websocket.SendResult;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;


@Component
@Slf4j
public class AutoSendOrder {
    @Autowired
    private PostPushService postPushService;
    @Autowired
    private PushMsgTypeService pushMsgTypeService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;

    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void topCheck() {
        List<PostPush> postPushList = postPushService.list(new QueryWrapper<PostPush>().eq("is_top", 1));
        for (PostPush postPush : postPushList) {
            if (postPush.getTopEndTime() != null) {
                if (LocalDateTime.now().isAfter(postPush.getTopEndTime())) {
                    PostPush push = new PostPush();
                    push.setIsTop(0);
                    push.setPostPushId(postPush.getPostPushId());
                    postPushService.updateById(push);
                }
            }
        }
    }

    //工作提醒
    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void sendTips() {
        List<PostPush> postPushList = postPushService.list(new QueryWrapper<PostPush>().eq("is_push", 0));
        String Template_id = commonInfoService.findOne(423).getValue();
        String appid = commonInfoService.findOne(45).getValue();

        for (PostPush postPush : postPushList) {
            Double kilometre = Double.valueOf(commonInfoService.findOne(422).getValue());

            List<PushMsgType> pushMsgTypeList = pushMsgTypeService.getMsgListByPostPush(postPush, kilometre);

            for (PushMsgType pushMsgType : pushMsgTypeList) {
                UserEntity userEntity = userService.getById(pushMsgType.getUserId());
                String str;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if (postPush.getContent().length() <= 20) {
                    str = postPush.getContent();
                } else {
                    str = postPush.getContent().substring(0, 16) + "...";
                }


                if (StringUtils.isNotBlank(userEntity.getAccountOpenId())) {
                    LinkedHashMap<String, TemplateMessageItem> data = new LinkedHashMap<>();
                    data.put("thing18", new TemplateMessageItem(str, "#d71345"));
                    data.put("thing16", new TemplateMessageItem(postPush.getProvince() + postPush.getCity(), "#d71345"));
                    data.put("time15", new TemplateMessageItem(sdf.format(new Date()), "#d71345"));
                    sendWxMessage(Template_id, data, userEntity.getAccountOpenId(), "/pages/index/index");
                }

            }


            postPush.setIsPush(1);
            postPushService.updateById(postPush);
        }
    }


    private void sendWxMessage(String templateId, LinkedHashMap<String, TemplateMessageItem> data, String openid, String url) {
        TemplateMessage templateMessage = new TemplateMessage();
        templateMessage.setTouser(openid);
        templateMessage.setTemplate_id(templateId);
        templateMessage.setData(data);
        templateMessage.setUrl(url);
        TemplateMessageResult templateMessageResult = MessageAPI.messageTemplateSend(SenInfoCheckUtil.getWxToken(), templateMessage);
        if (templateMessageResult.isSuccess()) {
            new SendResult();
        } else {
            new SendResult();
        }
    }

}
